package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 获取今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getTodayBusinessData(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setNewUsers(userMapper.getUserCountByTime(beginTime, endTime));
        Map map = new HashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        Integer allCount = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumAmountByTime(map);
        if(turnover == null)turnover = 0.0;
        businessDataVO.setTurnover(turnover);
        Integer validCount = orderMapper.countByMap(map);
        businessDataVO.setValidOrderCount(validCount);
        Double rate = 0.0;
        if(allCount != 0){
            rate = validCount/(double)allCount;
        }
        businessDataVO.setOrderCompletionRate(rate);
        rate = 0.0;
        if(validCount != 0){
            rate = turnover/(double)validCount;
        }
        businessDataVO.setUnitPrice(rate);
        return businessDataVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);

        return new SetmealOverViewVO(sold, discontinued);
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);

        return new DishOverViewVO(sold, discontinued);
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        return OrderOverViewVO.builder()
                .waitingOrders(orderMapper.countByStatus(Orders.TO_BE_CONFIRMED))
                .deliveredOrders(orderMapper.countByStatus(Orders.CONFIRMED))
                .completedOrders(orderMapper.countByStatus(Orders.COMPLETED))
                .cancelledOrders(orderMapper.countByStatus(Orders.CANCELLED))
                .allOrders(orderMapper.countByStatus(null))
                .build();
    }
}
